//
//  CHRootManage.swift
//  CHTabbar
//
//  Created by cyptt on 2022/7/1.
//

import UIKit

public class CHTabbarRootManage: NSObject {
    
    
    private static let loginCache = "chlogin"
    
    /*
     初始化
     */
    public static func  initRoot(loginVC:UIViewController,navClass:UINavigationController.Type)->UIViewController{
        
        let loginCache = UserDefaults.standard.object(forKey: loginCache)
        if loginCache == nil{
            let chlogin = loginVC
            return chlogin
        }else{
            let tabbar = CHTabBarController.init(navClass: navClass)
            tabbar.view.backgroundColor = UIColor.white
            return tabbar
        }
    }
    
    
    
    /*
     登录成功
     */
    public static func loginSuccess(navClass:UINavigationController.Type){
        let window:UIWindow = UIApplication.shared.keyWindow!
        let tabbar = CHTabBarController.init(navClass: navClass)
        tabbar.view.backgroundColor = UIColor.white
        window.rootViewController = tabbar
        UserDefaults.standard.set("loginSuccess", forKey: loginCache)
    }
    
    
    /*
     退出登录
     */
    public static func loginOut(loginVC:UIViewController){
        let window:UIWindow = UIApplication.shared.keyWindow!
        
        UserDefaults.standard.removeObject(forKey: loginCache)
        UserDefaults.standard.synchronize()
        window.rootViewController = loginVC
        
    }
}
